﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BackGroundWorker5
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// backgroundWorker 后台任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            // 这里是后台任务执行的代码，例如模拟长时间运行的任务：  
            for (int i = 0; i <= 100; i++)
            {
                if (backgroundWorker1.CancellationPending)
                {
                    e.Cancel = true; // 这里才真正取消 
                    return;
                }
                else
                {
                    Thread.Sleep(500); // 模拟耗时操作  
                    backgroundWorker1.ReportProgress(i, new Param() { Name = "A" + i, Value = i }); // 更新进度条（如果需要）  
                }
            }
        }

        /// <summary>
        /// backgroundWorker 后台任务进行中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // 更新进度条的代码，这里可以添加到你的 UI 控件中（例如一个ProgressBar）  
            progressBar1.Value = e.ProgressPercentage; // 假设有一个名为 progressBar1 的 ProgressBar 控件  
            label1.Text = (e.UserState as Param).ToString();
            label2.Text = $"运行进度{e.ProgressPercentage}";
        }

        /// <summary>
        /// backgroundWorker 后台任务完成了
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // 当后台操作完成时触发的代码，这里可以添加更新UI的代码或者通知用户操作的完成。  
            MessageBox.Show("任务完成"); // 显示一个消息框通知用户任务已完成。  
        }

        /// <summary>
        /// 开始
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            backgroundWorker1.RunWorkerAsync(); // 启动后台操作  
        }

        /// <summary>
        /// 取消任务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            // 开始取消
            if (backgroundWorker1.IsBusy) // 是否在运行异步操作 
            {
                backgroundWorker1.CancelAsync(); // (是)提交取消命令 
            }
        }
    }

    internal class Param
    {
        public string Name { get; set; }
        public int Value { get; set; }

        public override string ToString()
        {
            return $"Name:{Name},Value:{Value}";
        }
    }
}
